<?php
session_start();

function login() {
    $args = filter_input_array(INPUT_POST, array(
        'username' => FILTER_DEFAULT,
        'password' => FILTER_DEFAULT
    ));
    if ($args === null || $args === false || count(array_filter($args, function($arg) {
                        return $arg === false || $arg === null;
                    })) !== 0) {
        throw new Exception('args error');
    }
    if (isset($args['username']) && isset($args['password'])) {
        if ($args['username'] === "") {
            /* echo json_encode(array('result' => 1)); */
			throw new Exception('username null');
        }
        if ($args['password'] === "") {
            /* echo json_encode(array('result' => 2)); */
			throw new Exception('password null');
        }
        $connect = new CONNECT();
        $db = $connect->connecttodb();
        $action = $db->prepare("select * from admin where name = ?");
        $action->execute(array($args['username']));
        $result = $action->fetchAll();
        if (count($result) === 0) {
            throw new Exception('username error');
        }
        if (md5($args['password']) !== $result[0]['password']) {
            throw new Exception('password error');
        }
        $_SESSION["name"] = $args['username'];
        $_SESSION["status"] = true;
        if ($result[0]['root'] === '1') {
            $_SESSION['admin'] = true;
        } else {
            $_SESSION['admin'] = false;
        }
        return array();
    }
}

function addManager() {
	if($_SESSION['admin'] !== true)
	{
		throw new Exception('not login');
	}
    $args = filter_input_array(INPUT_POST, array(
        'name' => FILTER_DEFAULT,
        'password' => FILTER_DEFAULT,
        'duty' => FILTER_DEFAULT,
        'number' => FILTER_DEFAULT
    ));
    if ($args === null || $args === false || count(array_filter($args, function($arg) {
                        return $arg === false || $arg === null;
                    })) !== 0) {
        throw new Exception('参数错误！');
    }
    $connect = new CONNECT();
    $db = $connect->connecttodb();
    $action = $db->prepare("SELECT * FROM `admin` WHERE name = ?");
    $action->execute(array($args['name']));
    $admin = $action->fetchAll();
    if (count($admin)) {
        throw new Exception('管理员已存在！');
    }
    $db->prepare("INSERT INTO `admin`(`name`, `password`, `duty`, `number`, `root`) VALUES (?,?,?,?,0)")
            ->execute(array($args["name"], md5($args["password"]), $args["duty"], $args["number"]));
    return array();
}

function addBlacklist()
{
	if($_SESSION['status'] !== true)
	{
		throw new Exception('not login');
	}
    $arg = filter_input(INPUT_POST, 'name', FILTER_DEFAULT);
    if ($arg === null || $arg === false) 
    {
        throw new Exception('参数错误');
    }
    else
    {
        $connect = new CONNECT();
        $connect = $connect->connecttodb();
        $action = $connect->prepare("SELECT * FROM `blacklist` WHERE name = ?");
        $action->execute(array($arg));
        $result = $action->fetchAll();
        if (count($result)) {
            throw new Exception("该用户已在黑名单！");
        } 
        else 
        {
            $action = $connect->prepare("INSERT INTO `blacklist`(`name`) VALUES (?)");
            $result = $action->execute(array($arg));
            if ($result) {
                return array();
            }
        }
    }
}

/* function applySite() */
/* { */
/*     $args = filter_input_array(INPUT_POST, array( */
/*         'from_year' => FILTER_VALIDATE_INT, */
/*         'from_month' => FILTER_VALIDATE_INT, */
/*         'from_day' => FILTER_VALIDATE_INT, */
/*         'to_year' => FILTER_VALIDATE_INT, */
/*         'to_month' => FILTER_VALIDATE_INT, */
/*         'to_day' => FILTER_VALIDATE_INT, */
/*         'activity' => FILTER_DEFAULT, */
/*         'leader' => FILTER_DEFAULT, */
/*         'number' => FILTER_DEFAULT, */
/*         'sum_member' => FILTER_VALIDATE_INT, */
/*         'sum' => FILTER_VALIDATE_INT */
/*         )); */

/* } */

try {
    $method = filter_input(INPUT_GET, 'method');
    if (function_exists($method)) {
		include_once __DIR__."/../model/model.php";
		$result = call_user_func($method);
		echo json_encode(array_merge(array('result' => true), $result));
    } else {
		throw new Exception('no method');
	}
} catch (Exception $exc) {
    echo json_encode(array_merge(array('result' => false), array('msg' => $exc->getMessage())));
}
